package com.sixth.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixth.dao.DepartmentDao;
import com.sixth.entity.Department;
import com.sixth.entity.Staff;
import com.sixth.service.DepartmentService;
import com.sixth.util.CommonResult;
import com.sixth.util.LayTab;
import com.sixth.util.TableLayUi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service(value = "departmentService")
public class DepartmentServicelmpl implements DepartmentService {
    @Autowired
    private DepartmentDao departmentDao;
    @Autowired
    private HttpSession session;
    public TableLayUi findDepartmentByCondition(int page, int limit) {
        PageHelper.startPage(page,limit);
        List<Department>departments=departmentDao.selectDepartmentByCondition(page,limit);
        PageInfo<Department>pageInfo = new PageInfo<Department>(departments);
        return new TableLayUi(0,"",pageInfo.getTotal(),pageInfo.getList());
    }

    public CommonResult delete(Integer deptid) {
        int row = departmentDao.deleteByPrimaryKey(deptid);
        if (row>0){
            return new CommonResult(2000,"删除成功",null);
        }else {
            return new CommonResult(5000,"删除失败",null);
        }
    }

    public CommonResult update(Department department) {
        int row =   departmentDao.updateByPrimaryKey(department);
        if (row>0){
            return new CommonResult(2000,"修改成功",null);
        }else {
            return new CommonResult(5000,"修改失败",null);
        }
    }

    public LayTab sel(Staff staff, Integer deptid) {
        Staff st =(Staff)session.getAttribute("staff");
        Integer branchid =st.getBranchid();
        List<Department>sel =departmentDao.sel(staff,deptid,branchid);
        System.out.println(sel);
        //return sel.size()>0?new LayTab(sel.size(),sel):null;
        if (sel.size()>0){
            return new LayTab(sel.size(),sel);
        }
        return null;
    }

    public CommonResult insert(Department department) {
        int row =departmentDao.insert(department);
        if (row>0){
            return new CommonResult(2000,"添加成功",null);
        }else {
            return new CommonResult(5000,"添加失败",null);
        }
    }
    /*部门名校验*/

    public CommonResult getDeptName(String deptname) {
        List<Department> deptName = departmentDao.getDeptName(deptname);
        if(deptName.size()>0){
            return CommonResult.fail();
        }else {
            return CommonResult.success();
        }
    }
}
